import { AEvent } from "../../../../../Core/Module/EventSystem/IEvent";
import { EventAttribute } from "../../../../../Decorator/Decorator";
import { ETTask } from "../../../../../Support/Share/Lib/Async/ETTask";
import { EventType } from "../../../../Model/Share/Demo/EventType";
import { FUIGlobalComponent } from "../../../../ModelView/Client/Demo/Global/FUIGlobalComponent";

@EventAttribute(EventType.CreateSpine)
export class CreateSpine_CreateSpineView extends AEvent<EventType.CreateSpine> {

    async Run(args: EventType.CreateSpine) {
        let tsc = ETTask.Create<sp.SkeletonData>();
        cc.resources.load("Spine/dwarf_hunter/dwarf_hunter", sp.SkeletonData, (err: Error, asset: sp.SkeletonData) => {
            if (err) {
                tsc.SetException(err);
                return;
            }
            tsc.SetResult(asset);
        });
        let asset = await tsc;
        if (!asset) {
            return;
        }
        if (!asset.isValid) {
            return;
        }
        let go = new cc.Node();
        let spine = go.addComponent(sp.Skeleton);
        spine.skeletonData = asset;
        if (spine.findAnimation("idle")) {
            spine.defaultAnimation = "idle";
        }
        spine.loop = true;

        FUIGlobalComponent.Instance.Unit.addChild(go);
    }

}